package com.mall.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.common.result.Result;
import com.mall.system.entity.DictData;
import com.mall.system.service.DictDataService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 字典数据控制器
 */
@Tag(name = "字典数据管理")
@Slf4j
@RestController
@RequestMapping("/system/dictDatas")
public class DictDataController {
    
    @Autowired
    private DictDataService dictDataService;
    
    @Operation(summary = "字典数据列表")
    @GetMapping
    public Result<IPage<DictData>> list(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            DictData dictData) {
        try {
            IPage<DictData> page = new Page<>(pageNum, pageSize);
            QueryWrapper<DictData> queryWrapper = new QueryWrapper<>();
            
            // 添加查询条件
            if (dictData.getDictLabel() != null && !dictData.getDictLabel().isEmpty()) {
                queryWrapper.like("dict_label", dictData.getDictLabel());
            }
            if (dictData.getDictType() != null && !dictData.getDictType().isEmpty()) {
                queryWrapper.eq("dict_type", dictData.getDictType());
            }
            if (dictData.getStatus() != null) {
                queryWrapper.eq("status", dictData.getStatus());
            }
            
            queryWrapper.orderByAsc("dict_sort");
            
            IPage<DictData> result = dictDataService.page(page, queryWrapper);
            return Result.success(result);
        } catch (Exception e) {
            log.error("查询字典数据列表异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
    
    @Operation(summary = "根据字典数据ID获取字典数据")
    @GetMapping("/{dictCode}")
    public Result<DictData> getById(@PathVariable("dictCode") Long dictCode) {
        try {
            DictData dictData = dictDataService.getById(dictCode);
            if (dictData != null) {
                return Result.success(dictData);
            } else {
                return Result.failed("字典数据不存在");
            }
        } catch (Exception e) {
            log.error("查询字典数据异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
    
    @Operation(summary = "根据字典类型获取字典数据列表")
    @GetMapping("/type/{dictType}")
    public Result<List<DictData>> listByType(@PathVariable("dictType") String dictType) {
        try {
            List<DictData> dictDatas = dictDataService.selectDictDataByType(dictType);
            return Result.success(dictDatas);
        } catch (Exception e) {
            log.error("根据字典类型获取字典数据列表异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
    
    @Operation(summary = "新增字典数据")
    @PostMapping
    public Result<Boolean> add(@RequestBody DictData dictData) {
        try {
            boolean success = dictDataService.save(dictData);
            if (success) {
                return Result.success(true);
            } else {
                return Result.failed("新增字典数据失败");
            }
        } catch (Exception e) {
            log.error("新增字典数据异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
    
    @Operation(summary = "更新字典数据")
    @PutMapping
    public Result<Boolean> update(@RequestBody DictData dictData) {
        try {
            if (dictData.getDictCode() == null) {
                return Result.failed("字典数据ID不能为空");
            }
            
            boolean success = dictDataService.updateById(dictData);
            if (success) {
                return Result.success(true);
            } else {
                return Result.failed("更新字典数据失败");
            }
        } catch (Exception e) {
            log.error("更新字典数据异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
    
    @Operation(summary = "删除字典数据")
    @DeleteMapping("/{ids}")
    public Result<Boolean> delete(@PathVariable("ids") List<Long> ids) {
        try {
            boolean success = dictDataService.removeByIds(ids);
            if (success) {
                return Result.success(true);
            } else {
                return Result.failed("删除字典数据失败");
            }
        } catch (Exception e) {
            log.error("删除字典数据异常: ", e);
            return Result.failed(e.getMessage());
        }
    }
}